package com.springboot.mybatis.mapper;

import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper //与启动类的MapperScan二选一  相当于dao层操作数据库
public interface UserMapper {
    @Select("SELECT * FROM t_users WHERE id = #{id}")
    User selectUserById(Integer id);

    String selectUserByIdAndPwdStr = "SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE id = #{id}\n" +
            "          AND password = #{pwd}";

    @Select(selectUserByIdAndPwdStr)
    User selectUserByIdAndPwd(@Param("id") Integer id ,@Param("pwd") String pwd);  //注解参数绑定

    @Select(selectUserByIdAndPwdStr)
    User selectUserByIdAndPwd4(Map values);  //map参数绑定

    String selectUserByIdAndPwdStr2 = " SELECT id,name,password,sex,birthday,registTime\n" +
            "        FROM t_users\n" +
            "        WHERE id = #{id}\n" +
            "          AND password = #{password}";

    @Select(selectUserByIdAndPwdStr2)
    User selectUserByUserInfo(User user);  //对象参数绑定

    List<User> selectUsersByKeyword(String keyword);  //模糊查询

    @Delete("delete from t_users where id = #{id}")
    void deleteUser(@Param("id") Integer id);

    String updateStr = " UPDATE t_users SET name=#{name}, password=#{password}, sex=#{sex}, birthday=#{birthday}\n" +
            "    WHERE id = #{id}";
    @Update(updateStr)
    void updateUser(User user);  //修改

    String insertStr = "INSERT INTO t_users\n" +
            "        VALUES (#{id}, #{name}, #{password}, #{sex}, #{birthday}, #{registTime});";

    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")  //主键回填
    @Insert(insertStr)
    void insertUser(User user);

    @Select("select * from t_users where id = #{id}")
    Map<String,Object> selectMapByUserId(Integer id);
}